/*
 * land
 */
define(function(require) {
	var comm = require('sdk/server');
	var appInit = appcan.ls.val('appInit');

	$('#tolog').on('click', function(e) {
		e.preventDefault();
		appcan.openView({
			anim: 4
		}, 'member', 'login');
	});
	$('#toreg').on('click', function(e) {
		e.preventDefault();
		appcan.openView({
			anim: 4
		}, 'member', 'reg');
	});

	appcan.ready(function() {

		uexWindow.onPopoverLoadFinishInRootWnd = function(name, url) {
			//内嵌浏览器加载监听
			if (name === 'webView') {
				appcan.window.publish('webViewDone', 1);
			}
		};
		
		//监听网络状态
		appcan.listenNet();

		//预取数据
		comm.checkPreget();

		//存root地址，用于扩展页面
		appcan.ls.val('resRoot', seajs.root);
		//检查更新
		comm.checkUpdate(window.platform, true);
		//推送
		var pushable = appcan.ls.val('pushable');
		if (pushable === null) {
			appcan.ls.val('pushable', '1');
			pushable = 1;
		};
		if (window.uexJPush) {
			//接收通知
			uexJPush.onReceiveNotification = function(data) {
				comm.jpush(data);
			};
			//接收自定义消息
			uexJPush.onReceiveMessage = function(data) {
				comm.jpush(data);
			};
			//点击消息
			uexJPush.onReceiveNotificationOpen = function(json) {
				if (!appcan.ls.val('user')) {
					return
				}
				if (json.split) {
					json = JSON.parse(json);
				}
				var extras = json.extras ? json.extras : {};
				switch (extras.type) {
					case "order":
						//appcan.openView(extras.id, 'shop', 'orderDetail');
						break;
					default:
						appcan.openView(null, 'member', 'msgList');
				}

			}
			if (pushable === '0') {
				//alert('禁止推送');
				uexJPush.stopPush();
			}
		} else if (window.uexXGPush) {
			//初始化
			window.platform = uexWidgetOne.platformName.toLowerCase();

			if (window.platform == 'android') {
				uexXGPush.setAccessidAndKey(appcfg.plugin.xgpush.AndroidAccessid, appcfg.plugin.xgpush.AndroidKey);
			} else if (window.platform == 'ios') {
				uexXGPush.setAccessidAndKey(appcfg.plugin.xgpush.iOSAccessid, appcfg.plugin.xgpush.iOSKey);
			}
			uexXGPush.onTextMessageCb = function(opCode, dataType, data) {
				data = JSON.parse(data);
				alert("收到消息透传，内容：" + data.content);
			}
			uexXGPush.onNotificationShowedCb = function(opCode, dataType, data) {
				data = JSON.parse(data);
				alert("展示通知，标题：" + data.title + "，内容：" + data.content + "，自定义内容：" + data.customContent + "，打开的activity：" + data.activity + "，msgid：" + data.msgId);
			}
			uexXGPush.onNotificationCilckedCb = function(opCode, dataType, data) {
				data = JSON.parse(data);
				alert("展示通知，标题：" + data.title + "，内容：" + data.content + "，自定义内容：" + data.customContent + "，打开的activity：" + data.activity + "，msgid：" + data.msgId);
			}
			if (pushable === '0') {
				//alert('禁止推送');
				uexXGPush.unregisterPush();
			}
		};
		//缓存坐标
		var preGetGps = function(callback) {
			comm.getLocation(callback);
		};

		//app启动&恢复
		uexWidget.onResume = function() {
			preGetGps();
			comm.collection();
		};
		//检测登陆
		if (!!appcan.ls.val('user')) {
			appcan.openView({
				anim: 0
			}, 'home');
		} else {
			if (!appcfg.project.hasAlbum || appInit) {
				$('#buttons').addClass('show');
			} else {
				var canGo = false,
					welcomPage,
					dataImg = [{
						content: "index1.jpg"
					}, {
						content: "index2.jpg"
					}, {
						content: "index3.jpg"
					}],
					slideFunc = function() {
						welcomPage = new iSlider({
							dom: document.getElementById('Slider-wrapper'),
							data: dataImg,
							isAutoplay: false,
							plugins: ['dot'],
							onslidechange: function() {
								canGo = false;
							},
							onslidechanged: function(a, b, c) {
								if (c.slideIndex == dataImg.length - 1) {
									canGo = true;
								} else {
									canGo = false;
								}
							}
						});
						welcomPage.delegate('touchend', 'img', function() {
							setTimeout(function() {
								if (canGo) {
									$('#Slider-wrapper').remove();
									$('.islider-dot-wrap').remove();
									$('#buttons').addClass('show');
								}
							}, 0);
						});
					};
				$('#buttons').css('z-index', 1);
				if (dataImg.length) {
					if (window.iSlider) {
						slideFunc();
					} else {
						appcan.getScript('lib/islider', function() {
							slideFunc();
						}, {
							css: true
						});
					}
				}
			}
		};

		appcan.window.on('resume', function() {
			$('#buttons').addClass('show');
		});
		appcan.window.on('pause', function() {
			welcomPage && welcomPage.destroy();
		});

	});
});